﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using Museum.DBAccess;

public partial class MuseumDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlCountry.DataSource = DBAccess.GetAllCountries();
            ddlCountry.DataBind();

            ddlCountry.Items.FindByText("United States").Selected = true;

            BindMuseumData();

            BindControls();
        }
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindControls();
    }

    private void BindControls()
    {
        DataTable dtCities = new DataTable();
        try
        {
            //load cities from DB
            dtCities = DBAccess.GetCities(Convert.ToInt32(ddlCountry.SelectedValue), DateTime.Parse(Constants.CityMinDate));
            if (dtCities != null)
            {
                ddlCity.DataSource = dtCities;
                ddlCity.DataBind();
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    /// <summary>
    /// Binds my grid data.
    /// </summary>
    private void BindMuseumData()
    {
        DataTable dtMuseum = new DataTable();
        try
        {
            dtMuseum = DBAccess.GetMuseumById(Convert.ToInt32(Request.QueryString["MuseumID"]));

            if (dtMuseum != null && dtMuseum.Rows.Count > 0)
            {
                txtMuseumName.Text = dtMuseum.Rows[0]["MuseumName"].ToString();
                txtAddress.Text = dtMuseum.Rows[0]["MuseumAddress"].ToString();
                txtDetails.Text = dtMuseum.Rows[0]["MuseumInfo"].ToString();
                ddlCity.SelectedValue = dtMuseum.Rows[0]["CityId"].ToString();
                ddlPartner.SelectedValue = dtMuseum.Rows[0]["IsPartner"].ToString();
                txtMuseumLink.Text = dtMuseum.Rows[0]["MuseumLink"].ToString();
                ddlCountry.SelectedValue = dtMuseum.Rows[0]["CountryId"].ToString();
                ddlRating.SelectedValue = dtMuseum.Rows[0]["MuseumRating"].ToString();
            }
        }
        catch
        {
        }
        finally
        {
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int result = 0;
        int museumID = 0;
        try
        {
            if (Request.QueryString["MuseumID"] != null)
            {
                museumID = Convert.ToInt32(Request.QueryString["MuseumID"].ToString());
            }

            result = DBAccess.InsertOrUpdateMuseumDetails(museumID, txtMuseumName.Text.Trim(),
                        txtAddress.Text.Trim(), txtDetails.Text.Trim(), Convert.ToBoolean(Convert.ToInt32(ddlPartner.SelectedValue)), Convert.ToInt32(ddlCity.SelectedValue),
                        Convert.ToInt32(ddlRating.SelectedValue), txtMuseumLink.Text.Trim());

            if (museumID > 0)
            {
                Alert.ShowMessage("Museum updated successfully.");
                Response.Redirect("ManageMuseum.aspx", false);
            }
            else
            {
                Alert.ShowMessage("Museum inserted successfully.");
                Response.Redirect("ManageMuseum.aspx", false);
            }

            ClearControls();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void btnReset_Click(object sender, EventArgs e)
    {
        ClearControls();
    }

    private void ClearControls()
    {
        txtMuseumName.Text = "";
        txtAddress.Text = "";
        txtDetails.Text = "";
        txtMuseumLink.Text = "";
        ddlPartner.SelectedIndex = 0;
        ddlRating.SelectedIndex = 0;
    }
}